package com.yty.demo._4_oop.design_patterns.factory;

/**
 * @author: yangtaoyao
 * @time: 2020/1/10 10:38
 * @description: 工厂模式
 */
interface Shape {
    void draw();
}
class Circle implements Shape{

    @Override
    public void draw() {
        System.out.println("Circle-draw");
    }
}
class Square implements Shape{

    @Override
    public void draw() {
        System.out.println("Square-draw");
    }
}
class Rectangle implements Shape{

    @Override
    public void draw() {
        System.out.println("Rectangle-draw");
    }
}

class ShapeFactory{
    static Shape getShape(String className){
        if(className==null){
            return null;
        }
        switch (className){
            case "Circle":
                return new Circle();
            case "Square":
                return new Square();
            case "Rectangle":
                return new Rectangle();
            default:
                return null;
        }
    }
}
public class FactoryPatternDemo{
    public static void main(String[] args) {
        Shape shape=ShapeFactory.getShape(Circle.class.getSimpleName());
        if (shape!=null){
            shape.draw();
        }
        Shape shape2=ShapeFactory.getShape(Square.class.getSimpleName());
        if (shape2!=null){
            shape2.draw();
        }
    }
}